<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: seo_url.php 447 2014-01-19 12:03:40Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 447 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-19 12:03:40 +0000 (Sun, 19 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

if(file_exists('includes/local/configure.php') && filesize('includes/local/configure.php') > 0)
	include('includes/local/configure.php');
elseif(file_exists('includes/configure.php') && filesize('includes/configure.php') > 0)
	include('includes/configure.php');
elseif(!defined('MERCARI_INSTALLED')) {
	header('Location: installer/');
	exit;
} else {
	header('Location: installer/');
	exit;
}

if(!empty($_GET['linkurl']) && empty($_GET['error'])) {
	include(DIR_WS_CLASSES.'class.mercari_db.php');
	require(DIR_WS_INCLUDES.'database_tables.php');
	$db_url = new mercari_db();

	$data = $db_url->db_query("SELECT
									url_text,
									products_id, products_c_path,
									categories_id,
									blog_id,
									blog_cat,
									content_group,
									language_id
								FROM
									".TABLE_SEO_URL."
								WHERE
									url_text = ".$db_url->db_prepare(strtolower($_GET['linkurl']))."
								LIMIT 1", true);
	if(!$data->_numOfRows) {
		$link = $db_url->db_query("SELECT
										cn.file_name_php
									FROM
										".TABLE_SEO_URL_NAMES." AS cn
									JOIN
										".TABLE_SEO_URL_PERSONAL_LINKS." AS cp
									ON
										cp.url_text = ".$db_url->db_prepare(strtolower($_GET['linkurl']))."
									AND
										cn.file_name = cp.file_name 
									LIMIT 1", true);
	}

	function get_cpath($category) {
		global $db_url;

		$cPath = $category;
		while($category != '0') {
			$category = $db_url->db_query("SELECT parent_id FROM ".TABLE_CATEGORIES." WHERE categories_id = ".$db_url->db_prepare($category)." LIMIT 1", true);
			if($category->fields['parent_id'] != '0')
				$cPath = $category->fields['parent_id'].'_'.$cPath;

			if($category->fields['parent_id'] == 0)
				break;

			$category = $category->fields['parent_id'];
		}
		return $cPath;
	}

	unset($_GET['linkurl']);
	if(!empty($data->fields['products_id'])) {
		$_GET['products_id'] = $data->fields['products_id'];
		if($data->fields['products_c_path'] != '') $_GET['cPath'] = $data->fields['products_c_path'];
		$PHP_SELF = 'product_info.php';
		include('product_info.php');

	} elseif(!empty($data->fields['categories_id']))	{
		$_GET['cPath'] = get_cpath($data->fields['categories_id']);
		$PHP_SELF = 'index.php';
		include('index.php');

	} elseif (!empty($data->fields['content_group'])) {
		$_GET['coID'] = $data->fields['content_group'];
		$PHP_SELF = 'shop_content.php';
		include('shop_content.php');

	} elseif (!empty($data->fields['blog_id'])) 	{
		$_GET['blog_item'] = $data->fields['blog_id'];
		$PHP_SELF = 'blog.php';
		include('blog.php');

	} elseif (!empty($data->fields['blog_cat'])) {
		$_GET['blog_cat'] = $data->fields['blog_cat'];
		$PHP_SELF = 'blog.php';
		include('blog.php');

	} elseif ((!empty($data->fields['url_text'])) && (!empty($data->fields['language_id']))) {
		$PHP_SELF = 'index.php';
		include('index.php');
		
	} elseif(!empty($link->fields['file_name_php'])) {
		$aktuelle_datei = $link->fields['file_name_php'];
		$PHP_SELF = $link->fields['file_name_php'];
		include($link->fields['file_name_php']);
	
	} else {
		header('Location: '.HTTP_SERVER.DIR_WS_CATALOG.'404.php?error='.$_SERVER['REQUEST_URI']); exit;
	}
	
} elseif(!empty($_GET['error'])) {
	header('Location: '.HTTP_SERVER.DIR_WS_CATALOG.'404.php?error='.$_SERVER['REQUEST_URI']); exit;

} else {
	$PHP_SELF = 'index.php';
	include('index.php');
}